{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "03_Pandas",
      "version": "0.3.2",
      "provenance": [],
      "collapsed_sections": [],
      "toc_visible": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    }
  },
  "cells": [
    {
      "metadata": {
        "id": "bOChJSNXtC9g",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "# Pandas"
      ]
    },
    {
      "metadata": {
        "id": "OLIxEDq6VhvZ",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "<img src=\"https://raw.githubusercontent.com/GokuMohandas/practicalAI/master/images/logo.png\" width=150>\n",
        "\n",
        "In this notebook, we'll learn the basics of data analysis with the Python Pandas library.\n",
        "\n",
        "<img src=\"https://raw.githubusercontent.com/GokuMohandas/practicalAI/master/images/pandas.png\" width=500>\n",
        "\n",
        "\n"
      ]
    },
    {
      "metadata": {
        "id": "VoMq0eFRvugb",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "# Uploading the data"
      ]
    },
    {
      "metadata": {
        "id": "qWro5T5qTJJL",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "We're first going to get some data to play with. We're going to load the titanic dataset from the public link below."
      ]
    },
    {
      "metadata": {
        "id": "cdg5wEFcV6qA",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "import urllib"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "6FuyDUTFVY7J",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "# Upload data from GitHub to notebook's local drive\n",
        "url = \"https://raw.githubusercontent.com/GokuMohandas/practicalAI/master/data/titanic.csv\"\n",
        "response = urllib.request.urlopen(url)\n",
        "html = response.read()\n",
        "with open('titanic.csv', 'wb') as f:\n",
        "    f.write(html)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "TK3wsHCFhldU",
        "colab_type": "code",
        "outputId": "1d07b5bb-5c0e-418d-fe1c-b6529a629a09",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 85
        }
      },
      "cell_type": "code",
      "source": [
        "# Checking if the data was uploaded\n",
        "!ls -l "
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "total 96\n",
            "-rw-r--r-- 1 root root  7946 Oct  8 17:27 processed_titanic.csv\n",
            "drwxr-xr-x 2 root root  4096 Sep 28 23:32 sample_data\n",
            "-rw-r--r-- 1 root root 85153 Oct  8 17:48 titanic.csv\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "TL4rwLUSW9hV",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "# Loading the data"
      ]
    },
    {
      "metadata": {
        "id": "4EOXMnGHiLxM",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "Now that we have some data to play with, let's load it into a Pandas dataframe. Pandas is a great Python library for data analysis."
      ]
    },
    {
      "metadata": {
        "id": "-Zd-zoyjaaw2",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "import pandas as pd"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "ywaEF_0aQ023",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "# Read from CSV to Pandas DataFrame\n",
        "df = pd.read_csv(\"titanic.csv\", header=0)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "J79FUzZWQ-kx",
        "colab_type": "code",
        "outputId": "bcef6952-191c-47ee-ae84-c7a41ac85b24",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 204
        }
      },
      "cell_type": "code",
      "source": [
        "# First five items\n",
        "df.head()"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>pclass</th>\n",
              "      <th>name</th>\n",
              "      <th>sex</th>\n",
              "      <th>age</th>\n",
              "      <th>sibsp</th>\n",
              "      <th>parch</th>\n",
              "      <th>ticket</th>\n",
              "      <th>fare</th>\n",
              "      <th>cabin</th>\n",
              "      <th>embarked</th>\n",
              "      <th>survived</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>1</td>\n",
              "      <td>Allen, Miss. Elisabeth Walton</td>\n",
              "      <td>female</td>\n",
              "      <td>29.0000</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>24160</td>\n",
              "      <td>211.3375</td>\n",
              "      <td>B5</td>\n",
              "      <td>S</td>\n",
              "      <td>1</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>1</td>\n",
              "      <td>Allison, Master. Hudson Trevor</td>\n",
              "      <td>male</td>\n",
              "      <td>0.9167</td>\n",
              "      <td>1</td>\n",
              "      <td>2</td>\n",
              "      <td>113781</td>\n",
              "      <td>151.5500</td>\n",
              "      <td>C22 C26</td>\n",
              "      <td>S</td>\n",
              "      <td>1</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>1</td>\n",
              "      <td>Allison, Miss. Helen Loraine</td>\n",
              "      <td>female</td>\n",
              "      <td>2.0000</td>\n",
              "      <td>1</td>\n",
              "      <td>2</td>\n",
              "      <td>113781</td>\n",
              "      <td>151.5500</td>\n",
              "      <td>C22 C26</td>\n",
              "      <td>S</td>\n",
              "      <td>0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>1</td>\n",
              "      <td>Allison, Mr. Hudson Joshua Creighton</td>\n",
              "      <td>male</td>\n",
              "      <td>30.0000</td>\n",
              "      <td>1</td>\n",
              "      <td>2</td>\n",
              "      <td>113781</td>\n",
              "      <td>151.5500</td>\n",
              "      <td>C22 C26</td>\n",
              "      <td>S</td>\n",
              "      <td>0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>1</td>\n",
              "      <td>Allison, Mrs. Hudson J C (Bessie Waldo Daniels)</td>\n",
              "      <td>female</td>\n",
              "      <td>25.0000</td>\n",
              "      <td>1</td>\n",
              "      <td>2</td>\n",
              "      <td>113781</td>\n",
              "      <td>151.5500</td>\n",
              "      <td>C22 C26</td>\n",
              "      <td>S</td>\n",
              "      <td>0</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "   pclass                                             name     sex      age  \\\n",
              "0       1                    Allen, Miss. Elisabeth Walton  female  29.0000   \n",
              "1       1                   Allison, Master. Hudson Trevor    male   0.9167   \n",
              "2       1                     Allison, Miss. Helen Loraine  female   2.0000   \n",
              "3       1             Allison, Mr. Hudson Joshua Creighton    male  30.0000   \n",
              "4       1  Allison, Mrs. Hudson J C (Bessie Waldo Daniels)  female  25.0000   \n",
              "\n",
              "   sibsp  parch  ticket      fare    cabin embarked  survived  \n",
              "0      0      0   24160  211.3375       B5        S         1  \n",
              "1      1      2  113781  151.5500  C22 C26        S         1  \n",
              "2      1      2  113781  151.5500  C22 C26        S         0  \n",
              "3      1      2  113781  151.5500  C22 C26        S         0  \n",
              "4      1      2  113781  151.5500  C22 C26        S         0  "
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 9
        }
      ]
    },
    {
      "metadata": {
        "id": "qhYyM3iGRZ8W",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "These are the diferent features: \n",
        "* pclass: class of travel\n",
        "* name: full name of the passenger\n",
        "* sex: gender\n",
        "* age: numerical age\n",
        "* sibsp: # of siblings/spouse aboard\n",
        "* parch: number of parents/child aboard\n",
        "* ticket: ticket number\n",
        "* fare: cost of the ticket\n",
        "* cabin: location of room\n",
        "* emarked: port that the passenger embarked at (C - Cherbourg, S - Southampton, Q = Queenstown)\n",
        "* survived: survial metric (0 - died, 1 - survived)"
      ]
    },
    {
      "metadata": {
        "id": "NBx5VP8K_y6N",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "# Exploratory analysis"
      ]
    },
    {
      "metadata": {
        "id": "DD14WJ1G_zum",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "We're going to use the Pandas library and see how we can explore and process our data."
      ]
    },
    {
      "metadata": {
        "id": "thR28yTmASRr",
        "colab_type": "code",
        "outputId": "e986bbd4-41ac-4b4d-e79f-06bae8209c36",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 297
        }
      },
      "cell_type": "code",
      "source": [
        "# Describe features\n",
        "df.describe()"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>pclass</th>\n",
              "      <th>age</th>\n",
              "      <th>sibsp</th>\n",
              "      <th>parch</th>\n",
              "      <th>fare</th>\n",
              "      <th>survived</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>count</th>\n",
              "      <td>1309.000000</td>\n",
              "      <td>1046.000000</td>\n",
              "      <td>1309.000000</td>\n",
              "      <td>1309.000000</td>\n",
              "      <td>1308.000000</td>\n",
              "      <td>1309.000000</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>mean</th>\n",
              "      <td>2.294882</td>\n",
              "      <td>29.881135</td>\n",
              "      <td>0.498854</td>\n",
              "      <td>0.385027</td>\n",
              "      <td>33.295479</td>\n",
              "      <td>0.381971</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>std</th>\n",
              "      <td>0.837836</td>\n",
              "      <td>14.413500</td>\n",
              "      <td>1.041658</td>\n",
              "      <td>0.865560</td>\n",
              "      <td>51.758668</td>\n",
              "      <td>0.486055</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>min</th>\n",
              "      <td>1.000000</td>\n",
              "      <td>0.166700</td>\n",
              "      <td>0.000000</td>\n",
              "      <td>0.000000</td>\n",
              "      <td>0.000000</td>\n",
              "      <td>0.000000</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>25%</th>\n",
              "      <td>2.000000</td>\n",
              "      <td>21.000000</td>\n",
              "      <td>0.000000</td>\n",
              "      <td>0.000000</td>\n",
              "      <td>7.895800</td>\n",
              "      <td>0.000000</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>50%</th>\n",
              "      <td>3.000000</td>\n",
              "      <td>28.000000</td>\n",
              "      <td>0.000000</td>\n",
              "      <td>0.000000</td>\n",
              "      <td>14.454200</td>\n",
              "      <td>0.000000</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>75%</th>\n",
              "      <td>3.000000</td>\n",
              "      <td>39.000000</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>0.000000</td>\n",
              "      <td>31.275000</td>\n",
              "      <td>1.000000</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>max</th>\n",
              "      <td>3.000000</td>\n",
              "      <td>80.000000</td>\n",
              "      <td>8.000000</td>\n",
              "      <td>9.000000</td>\n",
              "      <td>512.329200</td>\n",
              "      <td>1.000000</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "            pclass          age        sibsp        parch         fare  \\\n",
              "count  1309.000000  1046.000000  1309.000000  1309.000000  1308.000000   \n",
              "mean      2.294882    29.881135     0.498854     0.385027    33.295479   \n",
              "std       0.837836    14.413500     1.041658     0.865560    51.758668   \n",
              "min       1.000000     0.166700     0.000000     0.000000     0.000000   \n",
              "25%       2.000000    21.000000     0.000000     0.000000     7.895800   \n",
              "50%       3.000000    28.000000     0.000000     0.000000    14.454200   \n",
              "75%       3.000000    39.000000     1.000000     0.000000    31.275000   \n",
              "max       3.000000    80.000000     8.000000     9.000000   512.329200   \n",
              "\n",
              "          survived  \n",
              "count  1309.000000  \n",
              "mean      0.381971  \n",
              "std       0.486055  \n",
              "min       0.000000  \n",
              "25%       0.000000  \n",
              "50%       0.000000  \n",
              "75%       1.000000  \n",
              "max       1.000000  "
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 10
        }
      ]
    },
    {
      "metadata": {
        "id": "Mn5HqS3XmzJs",
        "colab_type": "code",
        "outputId": "1fffd3e3-45f4-44a3-f3ea-1a0219e0457b",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 364
        }
      },
      "cell_type": "code",
      "source": [
        "# Histograms\n",
        "df[\"age\"].hist()"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "<matplotlib.axes._subplots.AxesSubplot at 0x7f88a5f7b4a8>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 11
        },
        {
          "output_type": "display_data",
          "data": {
            "image/png": "iVBORw0KGgoAAAANSUhEUgAAAeQAAAFKCAYAAADMuCxnAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4yLCBo\ndHRwOi8vbWF0cGxvd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5yW5a5fHHntMdXV1kqQrV65owIABeuKJJ/ptF0/c\nqWvnzp06f/587Hadw4cPT/ZIPdbQ0KDt27frypUr8vl8Gjx4sHbu3KnS0lLdvn1bQ4YM0datW5WW\nlpbsUeOqrKzUW2+9pW9961uxY9u2bVNZWZnrduns7NQvfvELXb16VZ2dnVq5cqVGjBihjRs3um6X\nz7z11lv6xje+oQkTJrhyj48//ljr169XS0uL7ty5o5UrVyo3N9eVu0ifvh0SDoclST/96U+Vl5fn\n2l0aGhr0+uuv6+DBg5I+fa//l7/8pT755BONHDlSr776apIn7Jm2tjZt2rRJ169f1927d7V69WoF\ng8F+28UTQQYAwO1cf8kaAAAvIMgAABhAkAEAMIAgAwBgAEEGAMAAggwAgAEEGQAAAwgyAAAG/B9C\nr3yxdV1VfwAAAABJRU5ErkJggg==\n",
            "text/plain": [
              "<matplotlib.figure.Figure at 0x7f88a5f780f0>"
            ]
          },
          "metadata": {
            "tags": []
          }
        }
      ]
    },
    {
      "metadata": {
        "id": "7illbHR1nLEF",
        "colab_type": "code",
        "outputId": "f0e07bde-de74-4ce1-bd89-beb48558e636",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        }
      },
      "cell_type": "code",
      "source": [
        "# Unique values\n",
        "df[\"embarked\"].unique()"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array(['S', 'C', nan, 'Q'], dtype=object)"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 12
        }
      ]
    },
    {
      "metadata": {
        "id": "BG1IMeV_hrqV",
        "colab_type": "code",
        "outputId": "029ed3dc-b21f-4b01-da8c-09c8d747ce04",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 119
        }
      },
      "cell_type": "code",
      "source": [
        "# Selecting data by feature\n",
        "df[\"name\"].head()"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "0                      Allen, Miss. Elisabeth Walton\n",
              "1                     Allison, Master. Hudson Trevor\n",
              "2                       Allison, Miss. Helen Loraine\n",
              "3               Allison, Mr. Hudson Joshua Creighton\n",
              "4    Allison, Mrs. Hudson J C (Bessie Waldo Daniels)\n",
              "Name: name, dtype: object"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 13
        }
      ]
    },
    {
      "metadata": {
        "id": "wPrRGLDtiZSp",
        "colab_type": "code",
        "outputId": "cd41130c-2fe6-4f35-ba12-95498816d971",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 204
        }
      },
      "cell_type": "code",
      "source": [
        "# Filtering\n",
        "df[df[\"sex\"]==\"female\"].head() # only the female data appear"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>pclass</th>\n",
              "      <th>name</th>\n",
              "      <th>sex</th>\n",
              "      <th>age</th>\n",
              "      <th>sibsp</th>\n",
              "      <th>parch</th>\n",
              "      <th>ticket</th>\n",
              "      <th>fare</th>\n",
              "      <th>cabin</th>\n",
              "      <th>embarked</th>\n",
              "      <th>survived</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>1</td>\n",
              "      <td>Allen, Miss. Elisabeth Walton</td>\n",
              "      <td>female</td>\n",
              "      <td>29.0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>24160</td>\n",
              "      <td>211.3375</td>\n",
              "      <td>B5</td>\n",
              "      <td>S</td>\n",
              "      <td>1</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>1</td>\n",
              "      <td>Allison, Miss. Helen Loraine</td>\n",
              "      <td>female</td>\n",
              "      <td>2.0</td>\n",
              "      <td>1</td>\n",
              "      <td>2</td>\n",
              "      <td>113781</td>\n",
              "      <td>151.5500</td>\n",
              "      <td>C22 C26</td>\n",
              "      <td>S</td>\n",
              "      <td>0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>1</td>\n",
              "      <td>Allison, Mrs. Hudson J C (Bessie Waldo Daniels)</td>\n",
              "      <td>female</td>\n",
              "      <td>25.0</td>\n",
              "      <td>1</td>\n",
              "      <td>2</td>\n",
              "      <td>113781</td>\n",
              "      <td>151.5500</td>\n",
              "      <td>C22 C26</td>\n",
              "      <td>S</td>\n",
              "      <td>0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>6</th>\n",
              "      <td>1</td>\n",
              "      <td>Andrews, Miss. Kornelia Theodosia</td>\n",
              "      <td>female</td>\n",
              "      <td>63.0</td>\n",
              "      <td>1</td>\n",
              "      <td>0</td>\n",
              "      <td>13502</td>\n",
              "      <td>77.9583</td>\n",
              "      <td>D7</td>\n",
              "      <td>S</td>\n",
              "      <td>1</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>8</th>\n",
              "      <td>1</td>\n",
              "      <td>Appleton, Mrs. Edward Dale (Charlotte Lamson)</td>\n",
              "      <td>female</td>\n",
              "      <td>53.0</td>\n",
              "      <td>2</td>\n",
              "      <td>0</td>\n",
              "      <td>11769</td>\n",
              "      <td>51.4792</td>\n",
              "      <td>C101</td>\n",
              "      <td>S</td>\n",
              "      <td>1</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "   pclass                                             name     sex   age  \\\n",
              "0       1                    Allen, Miss. Elisabeth Walton  female  29.0   \n",
              "2       1                     Allison, Miss. Helen Loraine  female   2.0   \n",
              "4       1  Allison, Mrs. Hudson J C (Bessie Waldo Daniels)  female  25.0   \n",
              "6       1                Andrews, Miss. Kornelia Theodosia  female  63.0   \n",
              "8       1    Appleton, Mrs. Edward Dale (Charlotte Lamson)  female  53.0   \n",
              "\n",
              "   sibsp  parch  ticket      fare    cabin embarked  survived  \n",
              "0      0      0   24160  211.3375       B5        S         1  \n",
              "2      1      2  113781  151.5500  C22 C26        S         0  \n",
              "4      1      2  113781  151.5500  C22 C26        S         0  \n",
              "6      1      0   13502   77.9583       D7        S         1  \n",
              "8      2      0   11769   51.4792     C101        S         1  "
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 14
        }
      ]
    },
    {
      "metadata": {
        "id": "FOuLeYIojMMH",
        "colab_type": "code",
        "outputId": "cb3353d1-51bf-4995-9a22-c0b5a9695925",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 204
        }
      },
      "cell_type": "code",
      "source": [
        "# Sorting\n",
        "df.sort_values(\"age\", ascending=False).head()"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>pclass</th>\n",
              "      <th>name</th>\n",
              "      <th>sex</th>\n",
              "      <th>age</th>\n",
              "      <th>sibsp</th>\n",
              "      <th>parch</th>\n",
              "      <th>ticket</th>\n",
              "      <th>fare</th>\n",
              "      <th>cabin</th>\n",
              "      <th>embarked</th>\n",
              "      <th>survived</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>14</th>\n",
              "      <td>1</td>\n",
              "      <td>Barkworth, Mr. Algernon Henry Wilson</td>\n",
              "      <td>male</td>\n",
              "      <td>80.0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>27042</td>\n",
              "      <td>30.0000</td>\n",
              "      <td>A23</td>\n",
              "      <td>S</td>\n",
              "      <td>1</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>61</th>\n",
              "      <td>1</td>\n",
              "      <td>Cavendish, Mrs. Tyrell William (Julia Florence...</td>\n",
              "      <td>female</td>\n",
              "      <td>76.0</td>\n",
              "      <td>1</td>\n",
              "      <td>0</td>\n",
              "      <td>19877</td>\n",
              "      <td>78.8500</td>\n",
              "      <td>C46</td>\n",
              "      <td>S</td>\n",
              "      <td>1</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1235</th>\n",
              "      <td>3</td>\n",
              "      <td>Svensson, Mr. Johan</td>\n",
              "      <td>male</td>\n",
              "      <td>74.0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>347060</td>\n",
              "      <td>7.7750</td>\n",
              "      <td>NaN</td>\n",
              "      <td>S</td>\n",
              "      <td>0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>135</th>\n",
              "      <td>1</td>\n",
              "      <td>Goldschmidt, Mr. George B</td>\n",
              "      <td>male</td>\n",
              "      <td>71.0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>PC 17754</td>\n",
              "      <td>34.6542</td>\n",
              "      <td>A5</td>\n",
              "      <td>C</td>\n",
              "      <td>0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>9</th>\n",
              "      <td>1</td>\n",
              "      <td>Artagaveytia, Mr. Ramon</td>\n",
              "      <td>male</td>\n",
              "      <td>71.0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>PC 17609</td>\n",
              "      <td>49.5042</td>\n",
              "      <td>NaN</td>\n",
              "      <td>C</td>\n",
              "      <td>0</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "      pclass                                               name     sex   age  \\\n",
              "14         1               Barkworth, Mr. Algernon Henry Wilson    male  80.0   \n",
              "61         1  Cavendish, Mrs. Tyrell William (Julia Florence...  female  76.0   \n",
              "1235       3                                Svensson, Mr. Johan    male  74.0   \n",
              "135        1                          Goldschmidt, Mr. George B    male  71.0   \n",
              "9          1                            Artagaveytia, Mr. Ramon    male  71.0   \n",
              "\n",
              "      sibsp  parch    ticket     fare cabin embarked  survived  \n",
              "14        0      0     27042  30.0000   A23        S         1  \n",
              "61        1      0     19877  78.8500   C46        S         1  \n",
              "1235      0      0    347060   7.7750   NaN        S         0  \n",
              "135       0      0  PC 17754  34.6542    A5        C         0  \n",
              "9         0      0  PC 17609  49.5042   NaN        C         0  "
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 15
        }
      ]
    },
    {
      "metadata": {
        "id": "v0TCbtSMjMO5",
        "colab_type": "code",
        "outputId": "d3139123-aa86-43c0-da33-d5c51d0a2596",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 142
        }
      },
      "cell_type": "code",
      "source": [
        "# Grouping\n",
        "survived_group = df.groupby(\"survived\")\n",
        "survived_group.mean()"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>pclass</th>\n",
              "      <th>age</th>\n",
              "      <th>sibsp</th>\n",
              "      <th>parch</th>\n",
              "      <th>fare</th>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>survived</th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>2.500618</td>\n",
              "      <td>30.545369</td>\n",
              "      <td>0.521632</td>\n",
              "      <td>0.328801</td>\n",
              "      <td>23.353831</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>1.962000</td>\n",
              "      <td>28.918228</td>\n",
              "      <td>0.462000</td>\n",
              "      <td>0.476000</td>\n",
              "      <td>49.361184</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "            pclass        age     sibsp     parch       fare\n",
              "survived                                                    \n",
              "0         2.500618  30.545369  0.521632  0.328801  23.353831\n",
              "1         1.962000  28.918228  0.462000  0.476000  49.361184"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 16
        }
      ]
    },
    {
      "metadata": {
        "id": "34LmckWDhdSA",
        "colab_type": "code",
        "outputId": "17e1a9e3-555b-4641-a0ec-486b924a3e46",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 221
        }
      },
      "cell_type": "code",
      "source": [
        "# Selecting row\n",
        "df.iloc[0, :] # iloc gets rows (or columns) at particular positions in the index (so it only takes integers)"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "pclass                                  1\n",
              "name        Allen, Miss. Elisabeth Walton\n",
              "sex                                female\n",
              "age                                    29\n",
              "sibsp                                   0\n",
              "parch                                   0\n",
              "ticket                              24160\n",
              "fare                              211.338\n",
              "cabin                                  B5\n",
              "embarked                                S\n",
              "survived                                1\n",
              "Name: 0, dtype: object"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 17
        }
      ]
    },
    {
      "metadata": {
        "id": "QrdXeuRdFkXB",
        "colab_type": "code",
        "outputId": "26a2833b-6932-4ec1-be16-b3bec609605d",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        }
      },
      "cell_type": "code",
      "source": [
        "# Selecting specific value\n",
        "df.iloc[0, 1]\n"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'Allen, Miss. Elisabeth Walton'"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 18
        }
      ]
    },
    {
      "metadata": {
        "id": "Rz35_-x2FkaL",
        "colab_type": "code",
        "outputId": "828a60ae-bf92-48e1-a504-ff2471a45948",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 221
        }
      },
      "cell_type": "code",
      "source": [
        "# Selecting by index\n",
        "df.loc[0] # loc gets rows (or columns) with particular labels from the index"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "pclass                                  1\n",
              "name        Allen, Miss. Elisabeth Walton\n",
              "sex                                female\n",
              "age                                    29\n",
              "sibsp                                   0\n",
              "parch                                   0\n",
              "ticket                              24160\n",
              "fare                              211.338\n",
              "cabin                                  B5\n",
              "embarked                                S\n",
              "survived                                1\n",
              "Name: 0, dtype: object"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 19
        }
      ]
    },
    {
      "metadata": {
        "id": "uSezrq4vEFYh",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "# Preprocessing"
      ]
    },
    {
      "metadata": {
        "id": "EZ1pCKHIjMUY",
        "colab_type": "code",
        "outputId": "bb1233b0-5930-4337-8a15-22337dda9f6f",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 204
        }
      },
      "cell_type": "code",
      "source": [
        "# Rows with at least one NaN value\n",
        "df[pd.isnull(df).any(axis=1)].head()"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>pclass</th>\n",
              "      <th>name</th>\n",
              "      <th>sex</th>\n",
              "      <th>age</th>\n",
              "      <th>sibsp</th>\n",
              "      <th>parch</th>\n",
              "      <th>ticket</th>\n",
              "      <th>fare</th>\n",
              "      <th>cabin</th>\n",
              "      <th>embarked</th>\n",
              "      <th>survived</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>9</th>\n",
              "      <td>1</td>\n",
              "      <td>Artagaveytia, Mr. Ramon</td>\n",
              "      <td>male</td>\n",
              "      <td>71.0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>PC 17609</td>\n",
              "      <td>49.5042</td>\n",
              "      <td>NaN</td>\n",
              "      <td>C</td>\n",
              "      <td>0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>13</th>\n",
              "      <td>1</td>\n",
              "      <td>Barber, Miss. Ellen \"Nellie\"</td>\n",
              "      <td>female</td>\n",
              "      <td>26.0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>19877</td>\n",
              "      <td>78.8500</td>\n",
              "      <td>NaN</td>\n",
              "      <td>S</td>\n",
              "      <td>1</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>15</th>\n",
              "      <td>1</td>\n",
              "      <td>Baumann, Mr. John D</td>\n",
              "      <td>male</td>\n",
              "      <td>NaN</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>PC 17318</td>\n",
              "      <td>25.9250</td>\n",
              "      <td>NaN</td>\n",
              "      <td>S</td>\n",
              "      <td>0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>23</th>\n",
              "      <td>1</td>\n",
              "      <td>Bidois, Miss. Rosalie</td>\n",
              "      <td>female</td>\n",
              "      <td>42.0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>PC 17757</td>\n",
              "      <td>227.5250</td>\n",
              "      <td>NaN</td>\n",
              "      <td>C</td>\n",
              "      <td>1</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>25</th>\n",
              "      <td>1</td>\n",
              "      <td>Birnbaum, Mr. Jakob</td>\n",
              "      <td>male</td>\n",
              "      <td>25.0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>13905</td>\n",
              "      <td>26.0000</td>\n",
              "      <td>NaN</td>\n",
              "      <td>C</td>\n",
              "      <td>0</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "    pclass                          name     sex   age  sibsp  parch  \\\n",
              "9        1       Artagaveytia, Mr. Ramon    male  71.0      0      0   \n",
              "13       1  Barber, Miss. Ellen \"Nellie\"  female  26.0      0      0   \n",
              "15       1           Baumann, Mr. John D    male   NaN      0      0   \n",
              "23       1         Bidois, Miss. Rosalie  female  42.0      0      0   \n",
              "25       1           Birnbaum, Mr. Jakob    male  25.0      0      0   \n",
              "\n",
              "      ticket      fare cabin embarked  survived  \n",
              "9   PC 17609   49.5042   NaN        C         0  \n",
              "13     19877   78.8500   NaN        S         1  \n",
              "15  PC 17318   25.9250   NaN        S         0  \n",
              "23  PC 17757  227.5250   NaN        C         1  \n",
              "25     13905   26.0000   NaN        C         0  "
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 20
        }
      ]
    },
    {
      "metadata": {
        "id": "zUaiFplEkmoB",
        "colab_type": "code",
        "outputId": "3caed8fa-5c95-4d44-a0f8-b4554ad3e009",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 204
        }
      },
      "cell_type": "code",
      "source": [
        "# Drop rows with Nan values\n",
        "df = df.dropna() # removes rows with any NaN values\n",
        "df = df.reset_index() # reset's row indexes in case any rows were dropped\n",
        "df.head()"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>index</th>\n",
              "      <th>pclass</th>\n",
              "      <th>name</th>\n",
              "      <th>sex</th>\n",
              "      <th>age</th>\n",
              "      <th>sibsp</th>\n",
              "      <th>parch</th>\n",
              "      <th>ticket</th>\n",
              "      <th>fare</th>\n",
              "      <th>cabin</th>\n",
              "      <th>embarked</th>\n",
              "      <th>survived</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "      <td>Allen, Miss. Elisabeth Walton</td>\n",
              "      <td>female</td>\n",
              "      <td>29.0000</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>24160</td>\n",
              "      <td>211.3375</td>\n",
              "      <td>B5</td>\n",
              "      <td>S</td>\n",
              "      <td>1</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>1</td>\n",
              "      <td>1</td>\n",
              "      <td>Allison, Master. Hudson Trevor</td>\n",
              "      <td>male</td>\n",
              "      <td>0.9167</td>\n",
              "      <td>1</td>\n",
              "      <td>2</td>\n",
              "      <td>113781</td>\n",
              "      <td>151.5500</td>\n",
              "      <td>C22 C26</td>\n",
              "      <td>S</td>\n",
              "      <td>1</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>2</td>\n",
              "      <td>1</td>\n",
              "      <td>Allison, Miss. Helen Loraine</td>\n",
              "      <td>female</td>\n",
              "      <td>2.0000</td>\n",
              "      <td>1</td>\n",
              "      <td>2</td>\n",
              "      <td>113781</td>\n",
              "      <td>151.5500</td>\n",
              "      <td>C22 C26</td>\n",
              "      <td>S</td>\n",
              "      <td>0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>3</td>\n",
              "      <td>1</td>\n",
              "      <td>Allison, Mr. Hudson Joshua Creighton</td>\n",
              "      <td>male</td>\n",
              "      <td>30.0000</td>\n",
              "      <td>1</td>\n",
              "      <td>2</td>\n",
              "      <td>113781</td>\n",
              "      <td>151.5500</td>\n",
              "      <td>C22 C26</td>\n",
              "      <td>S</td>\n",
              "      <td>0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>4</td>\n",
              "      <td>1</td>\n",
              "      <td>Allison, Mrs. Hudson J C (Bessie Waldo Daniels)</td>\n",
              "      <td>female</td>\n",
              "      <td>25.0000</td>\n",
              "      <td>1</td>\n",
              "      <td>2</td>\n",
              "      <td>113781</td>\n",
              "      <td>151.5500</td>\n",
              "      <td>C22 C26</td>\n",
              "      <td>S</td>\n",
              "      <td>0</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "   index  pclass                                             name     sex  \\\n",
              "0      0       1                    Allen, Miss. Elisabeth Walton  female   \n",
              "1      1       1                   Allison, Master. Hudson Trevor    male   \n",
              "2      2       1                     Allison, Miss. Helen Loraine  female   \n",
              "3      3       1             Allison, Mr. Hudson Joshua Creighton    male   \n",
              "4      4       1  Allison, Mrs. Hudson J C (Bessie Waldo Daniels)  female   \n",
              "\n",
              "       age  sibsp  parch  ticket      fare    cabin embarked  survived  \n",
              "0  29.0000      0      0   24160  211.3375       B5        S         1  \n",
              "1   0.9167      1      2  113781  151.5500  C22 C26        S         1  \n",
              "2   2.0000      1      2  113781  151.5500  C22 C26        S         0  \n",
              "3  30.0000      1      2  113781  151.5500  C22 C26        S         0  \n",
              "4  25.0000      1      2  113781  151.5500  C22 C26        S         0  "
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 21
        }
      ]
    },
    {
      "metadata": {
        "id": "ubujZv_8qG-d",
        "colab_type": "code",
        "outputId": "087deb79-c891-4cb9-cc9a-dbd1df82edbe",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 204
        }
      },
      "cell_type": "code",
      "source": [
        "# Dropping multiple columns\n",
        "df = df.drop([\"name\", \"cabin\", \"ticket\"], axis=1) # we won't use text features for our initial basic models\n",
        "df.head()"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>index</th>\n",
              "      <th>pclass</th>\n",
              "      <th>sex</th>\n",
              "      <th>age</th>\n",
              "      <th>sibsp</th>\n",
              "      <th>parch</th>\n",
              "      <th>fare</th>\n",
              "      <th>embarked</th>\n",
              "      <th>survived</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "      <td>female</td>\n",
              "      <td>29.0000</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>211.3375</td>\n",
              "      <td>S</td>\n",
              "      <td>1</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>1</td>\n",
              "      <td>1</td>\n",
              "      <td>male</td>\n",
              "      <td>0.9167</td>\n",
              "      <td>1</td>\n",
              "      <td>2</td>\n",
              "      <td>151.5500</td>\n",
              "      <td>S</td>\n",
              "      <td>1</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>2</td>\n",
              "      <td>1</td>\n",
              "      <td>female</td>\n",
              "      <td>2.0000</td>\n",
              "      <td>1</td>\n",
              "      <td>2</td>\n",
              "      <td>151.5500</td>\n",
              "      <td>S</td>\n",
              "      <td>0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>3</td>\n",
              "      <td>1</td>\n",
              "      <td>male</td>\n",
              "      <td>30.0000</td>\n",
              "      <td>1</td>\n",
              "      <td>2</td>\n",
              "      <td>151.5500</td>\n",
              "      <td>S</td>\n",
              "      <td>0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>4</td>\n",
              "      <td>1</td>\n",
              "      <td>female</td>\n",
              "      <td>25.0000</td>\n",
              "      <td>1</td>\n",
              "      <td>2</td>\n",
              "      <td>151.5500</td>\n",
              "      <td>S</td>\n",
              "      <td>0</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "   index  pclass     sex      age  sibsp  parch      fare embarked  survived\n",
              "0      0       1  female  29.0000      0      0  211.3375        S         1\n",
              "1      1       1    male   0.9167      1      2  151.5500        S         1\n",
              "2      2       1  female   2.0000      1      2  151.5500        S         0\n",
              "3      3       1    male  30.0000      1      2  151.5500        S         0\n",
              "4      4       1  female  25.0000      1      2  151.5500        S         0"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 22
        }
      ]
    },
    {
      "metadata": {
        "id": "8m117GcVnon9",
        "colab_type": "code",
        "outputId": "103c4eec-b38c-4921-af84-af6f955aba90",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 204
        }
      },
      "cell_type": "code",
      "source": [
        "# Map feature values\n",
        "df['sex'] = df['sex'].map( {'female': 0, 'male': 1} ).astype(int)\n",
        "df[\"embarked\"] = df['embarked'].dropna().map( {'S':0, 'C':1, 'Q':2} ).astype(int)\n",
        "df.head()"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>index</th>\n",
              "      <th>pclass</th>\n",
              "      <th>sex</th>\n",
              "      <th>age</th>\n",
              "      <th>sibsp</th>\n",
              "      <th>parch</th>\n",
              "      <th>fare</th>\n",
              "      <th>embarked</th>\n",
              "      <th>survived</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "      <td>0</td>\n",
              "      <td>29.0000</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>211.3375</td>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>1</td>\n",
              "      <td>1</td>\n",
              "      <td>1</td>\n",
              "      <td>0.9167</td>\n",
              "      <td>1</td>\n",
              "      <td>2</td>\n",
              "      <td>151.5500</td>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>2</td>\n",
              "      <td>1</td>\n",
              "      <td>0</td>\n",
              "      <td>2.0000</td>\n",
              "      <td>1</td>\n",
              "      <td>2</td>\n",
              "      <td>151.5500</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>3</td>\n",
              "      <td>1</td>\n",
              "      <td>1</td>\n",
              "      <td>30.0000</td>\n",
              "      <td>1</td>\n",
              "      <td>2</td>\n",
              "      <td>151.5500</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>4</td>\n",
              "      <td>1</td>\n",
              "      <td>0</td>\n",
              "      <td>25.0000</td>\n",
              "      <td>1</td>\n",
              "      <td>2</td>\n",
              "      <td>151.5500</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "   index  pclass  sex      age  sibsp  parch      fare  embarked  survived\n",
              "0      0       1    0  29.0000      0      0  211.3375         0         1\n",
              "1      1       1    1   0.9167      1      2  151.5500         0         1\n",
              "2      2       1    0   2.0000      1      2  151.5500         0         0\n",
              "3      3       1    1  30.0000      1      2  151.5500         0         0\n",
              "4      4       1    0  25.0000      1      2  151.5500         0         0"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 23
        }
      ]
    },
    {
      "metadata": {
        "id": "ZaVqjpsCEtft",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "# Feature engineering"
      ]
    },
    {
      "metadata": {
        "id": "_FPtk5tpqrDI",
        "colab_type": "code",
        "outputId": "faf26f41-50f4-4cdf-fd52-46fadee89090",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 204
        }
      },
      "cell_type": "code",
      "source": [
        "# Lambda expressions to create new features\n",
        "def get_family_size(sibsp, parch):\n",
        "    family_size = sibsp + parch\n",
        "    return family_size\n",
        "\n",
        "df[\"family_size\"] = df[[\"sibsp\", \"parch\"]].apply(lambda x: get_family_size(x[\"sibsp\"], x[\"parch\"]), axis=1)\n",
        "df.head()"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>index</th>\n",
              "      <th>pclass</th>\n",
              "      <th>sex</th>\n",
              "      <th>age</th>\n",
              "      <th>sibsp</th>\n",
              "      <th>parch</th>\n",
              "      <th>fare</th>\n",
              "      <th>embarked</th>\n",
              "      <th>survived</th>\n",
              "      <th>family_size</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "      <td>0</td>\n",
              "      <td>29.0000</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>211.3375</td>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "      <td>0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>1</td>\n",
              "      <td>1</td>\n",
              "      <td>1</td>\n",
              "      <td>0.9167</td>\n",
              "      <td>1</td>\n",
              "      <td>2</td>\n",
              "      <td>151.5500</td>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "      <td>3</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>2</td>\n",
              "      <td>1</td>\n",
              "      <td>0</td>\n",
              "      <td>2.0000</td>\n",
              "      <td>1</td>\n",
              "      <td>2</td>\n",
              "      <td>151.5500</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>3</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>3</td>\n",
              "      <td>1</td>\n",
              "      <td>1</td>\n",
              "      <td>30.0000</td>\n",
              "      <td>1</td>\n",
              "      <td>2</td>\n",
              "      <td>151.5500</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>3</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>4</td>\n",
              "      <td>1</td>\n",
              "      <td>0</td>\n",
              "      <td>25.0000</td>\n",
              "      <td>1</td>\n",
              "      <td>2</td>\n",
              "      <td>151.5500</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>3</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "   index  pclass  sex      age  sibsp  parch      fare  embarked  survived  \\\n",
              "0      0       1    0  29.0000      0      0  211.3375         0         1   \n",
              "1      1       1    1   0.9167      1      2  151.5500         0         1   \n",
              "2      2       1    0   2.0000      1      2  151.5500         0         0   \n",
              "3      3       1    1  30.0000      1      2  151.5500         0         0   \n",
              "4      4       1    0  25.0000      1      2  151.5500         0         0   \n",
              "\n",
              "   family_size  \n",
              "0            0  \n",
              "1            3  \n",
              "2            3  \n",
              "3            3  \n",
              "4            3  "
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 24
        }
      ]
    },
    {
      "metadata": {
        "id": "JK3FqfjnpSNi",
        "colab_type": "code",
        "outputId": "7cc0f028-b924-4b17-c872-408f3fbcf368",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 204
        }
      },
      "cell_type": "code",
      "source": [
        "# Reorganize headers\n",
        "df = df[['pclass', 'sex', 'age', 'sibsp', 'parch', 'family_size', 'fare', 'embarked', 'survived']]\n",
        "df.head()"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>pclass</th>\n",
              "      <th>sex</th>\n",
              "      <th>age</th>\n",
              "      <th>sibsp</th>\n",
              "      <th>parch</th>\n",
              "      <th>family_size</th>\n",
              "      <th>fare</th>\n",
              "      <th>embarked</th>\n",
              "      <th>survived</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>1</td>\n",
              "      <td>0</td>\n",
              "      <td>29.0000</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>211.3375</td>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>1</td>\n",
              "      <td>1</td>\n",
              "      <td>0.9167</td>\n",
              "      <td>1</td>\n",
              "      <td>2</td>\n",
              "      <td>3</td>\n",
              "      <td>151.5500</td>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>1</td>\n",
              "      <td>0</td>\n",
              "      <td>2.0000</td>\n",
              "      <td>1</td>\n",
              "      <td>2</td>\n",
              "      <td>3</td>\n",
              "      <td>151.5500</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>1</td>\n",
              "      <td>1</td>\n",
              "      <td>30.0000</td>\n",
              "      <td>1</td>\n",
              "      <td>2</td>\n",
              "      <td>3</td>\n",
              "      <td>151.5500</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>1</td>\n",
              "      <td>0</td>\n",
              "      <td>25.0000</td>\n",
              "      <td>1</td>\n",
              "      <td>2</td>\n",
              "      <td>3</td>\n",
              "      <td>151.5500</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "   pclass  sex      age  sibsp  parch  family_size      fare  embarked  \\\n",
              "0       1    0  29.0000      0      0            0  211.3375         0   \n",
              "1       1    1   0.9167      1      2            3  151.5500         0   \n",
              "2       1    0   2.0000      1      2            3  151.5500         0   \n",
              "3       1    1  30.0000      1      2            3  151.5500         0   \n",
              "4       1    0  25.0000      1      2            3  151.5500         0   \n",
              "\n",
              "   survived  \n",
              "0         1  \n",
              "1         1  \n",
              "2         0  \n",
              "3         0  \n",
              "4         0  "
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 25
        }
      ]
    },
    {
      "metadata": {
        "id": "N_rwgfrFGTne",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "# Saving data"
      ]
    },
    {
      "metadata": {
        "id": "rNNxA7Vrp2fC",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "# Saving dataframe to CSV\n",
        "df.to_csv(\"processed_titanic.csv\", index=False)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "gfc7Epp7sgqz",
        "colab_type": "code",
        "outputId": "568781d5-b0c9-44ec-f4d2-b767c291ff61",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 85
        }
      },
      "cell_type": "code",
      "source": [
        "# See your saved file\n",
        "!ls -l"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "total 96\n",
            "-rw-r--r-- 1 root root  6975 Oct  8 17:49 processed_titanic.csv\n",
            "drwxr-xr-x 2 root root  4096 Sep 28 23:32 sample_data\n",
            "-rw-r--r-- 1 root root 85153 Oct  8 17:48 titanic.csv\n"
          ],
          "name": "stdout"
        }
      ]
    }
  ]
}